package com.just521.xinshoutong.http;

import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.just521.xinshoutong.R;
import com.just521.xinshoutong.common.CommonDefine;
import com.just521.xinshoutong.http.HttpHelperWraper.HttpRequestParam;
import com.just521.xinshoutong.http.HttpHelperWraper.OnNetListener;



public class NetHelperBase 
implements OnNetListener{
	
	protected 	static Activity 				m_Context=null;
    private 	EventHandler 					mHandler = null; 
	protected 	HashMap<String,Object> 			info = null;
	private		HashMap<String, Object> 	   	requestparams = new HashMap<String, Object>();
	public 		boolean  						show_dlg = false;
	private 	ProgressDialog 					mypDialog = null;
    public		Bitmap							image=null;
	
    public static final int 				PROGRESS_DIALOG = 1; 
    public static final int 				PROGRESS_DIALOG_CANCLE = 2; 
    public static final int 				PROGRESS_DIALOG_ShowERR = 3; 
    HttpHelperWraper 						http;
    
    

	
	public NetHelperBase(Activity context, boolean  show_wait_dlg){
		m_Context = context;

	    mHandler = new EventHandler(this);
	    show_dlg = show_wait_dlg;
		http= new HttpHelperWraper();
		
        init();
	}

	public void init()
	{
        if(info != null) info.clear();
        if(requestparams != null) 
        	requestparams.clear();
        else
        	requestparams = new HashMap<String, Object>();
	}

	public void insert_param(String key, Object value)
	{
		if(value != null && !value.equals(""))
			requestparams.put(key, value);
	}
	
	
	public void set_bitmap(Bitmap img){
		image = img;
	}
	
	
	public void request_server_yibu(String task)
	{
		HttpRequestParam param = http.getParam();
		param.task=task;
		param.ctx=m_Context;
		param.listener=this;
		Date date = new Date();
		param.ltimestamp = date.getTime();
		param.nodesrequest = requestparams;
//		param.headimage = image;
		http.AsyncTask(param);
        Message msg = mHandler.obtainMessage(PROGRESS_DIALOG); 
        mHandler.sendMessage(msg);
	}
	
	
	
	public void request_server_liner(String task){
		HttpRequestParam param = http.getParam();
		param.task=task;
		param.ctx=m_Context;
		param.listener=this;
		param.nodesrequest = requestparams;
		Date date = new Date();
		param.ltimestamp = date.getTime();
//		param.headimage = image;
		http.LinearAsyncTask(param);	
        Message msg = mHandler.obtainMessage(PROGRESS_DIALOG); 
        mHandler.sendMessage(msg);
	}
	
	
	
	public void request_server_liner_list(String task){
		HttpRequestParam param = http.getParam();
		param.task=task;
		param.ctx=m_Context;
		param.listener=this;
		param.nodesrequest = requestparams;
		Date date = new Date();
		param.ltimestamp = date.getTime();
//		param.headimage = image;
		Log.e("zhaodsh", "getlist"+String.valueOf(param.ltimestamp));
		http.LinearAsyncTask(param);
        Message msg = mHandler.obtainMessage(PROGRESS_DIALOG); 
        mHandler.sendMessage(msg);
	}
	

	@Override
	public void onNetSuccess(HttpRequestParam param) {
	    Message msg = mHandler.obtainMessage(PROGRESS_DIALOG_CANCLE); 
	    mHandler.sendMessage(msg);
		
	    process_result( param);	
	}

	@Override
	public void onNetError(HttpRequestParam param) {
	
		String  err_msg = (String)param.rstmsg;		
        Message msg = mHandler.obtainMessage(PROGRESS_DIALOG_ShowERR); 
        mHandler.sendMessage(msg);
        
 		Toast  t = Toast.makeText(m_Context, err_msg, 2000);
 		t.show();		
		
 		process_error(err_msg);
		
	}      
	
	
	

	public void process_result(HttpRequestParam param)
	{
		
		
	}
	public  void process_error(String err)
	{
		
	}
	

	public Object getObjest(String key)
	{
		if(info != null ) 
			return (Object)info.get(key);
		else
			return null;
	}
	
	public String getString(String key)
	{
		if(info != null ) 
			return (String)info.get(key);
		else
			return "";
	}
	public int getInt(String key)
	{
		if(info == null) return 0;
		
		String temp = (String)info.get(key);
		if(temp != null && !temp.equals(""))
			return Integer.valueOf(temp);
		else
			return 0;
	}
	public long getLong(String key)
	{
		if(info == null) return 0;
		
		String temp = (String)info.get(key);
		if(temp != null && !temp.equals(""))
			return Long.valueOf(temp);
		else
			return 0;
	}
	
	public String getStringContent(int id ){
		return CommonDefine.getResources(m_Context).getString(id);
	}
	
	
	
 
    private class EventHandler extends Handler {  
        private NetHelperBase mManager;  
  
        public EventHandler(NetHelperBase userInfoHelper) {  
        	mManager = userInfoHelper;
        }
        @Override  
        public void handleMessage(Message msg) {
            switch (msg.what) {  
            case PROGRESS_DIALOG:
            	if(show_dlg){
                	mypDialog = ProgressDialog.show(m_Context, "",  CommonDefine.getResources(m_Context).getString(R.string.toast_wait), true);
                	mypDialog.setCancelable(true);            		
            	}
                break;
            case PROGRESS_DIALOG_CANCLE:
            	if(mypDialog != null && show_dlg){
            		if(mypDialog.isShowing())
            		{
                    	mypDialog.cancel();		
            		}          		
            	}
                break;
            case PROGRESS_DIALOG_ShowERR:
             	if(mypDialog != null && show_dlg){
            		if(mypDialog.isShowing())
            		{
                    	mypDialog.cancel();		
            		}          		
            	}
            	break;
			default:  
                break;  
            }  
        }  
    }




}
